using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using FreeFlow.Administration;
using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for SqlLogControl.
	/// </summary>
	public class SqlLogControl : AdminUserControl
	{
    private System.Windows.Forms.ListView sqlListBox;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private System.Windows.Forms.ColumnHeader columnHeader1;
    private System.Windows.Forms.ColumnHeader columnHeader2;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SqlLogControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Copy to clipboard", "Copy the SQL log to the clipboard", new ActionExecuteDelegate(CopyToClipboard));
      actionList.Add("Execute selected", "Execute the selected row in the SQL Windows", new ActionExecuteDelegate(OnExecuteSelected));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

    public override bool CanDispose
    {
      get
      {
        return false;
      }
    }

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.sqlListBox = new System.Windows.Forms.ListView();
      this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
      this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      this.SuspendLayout();
      // 
      // sqlListBox
      // 
      this.sqlListBox.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                 this.columnHeader1,
                                                                                 this.columnHeader2});
      this.sqlListBox.Dock = System.Windows.Forms.DockStyle.Fill;
      this.sqlListBox.FullRowSelect = true;
      this.sqlListBox.Location = new System.Drawing.Point(0, 0);
      this.sqlListBox.Name = "sqlListBox";
      this.sqlListBox.Size = new System.Drawing.Size(466, 316);
      this.sqlListBox.TabIndex = 0;
      this.sqlListBox.View = System.Windows.Forms.View.Details;
      // 
      // columnHeader1
      // 
      this.columnHeader1.Text = "SQL";
      this.columnHeader1.Width = 400;
      // 
      // columnHeader2
      // 
      this.columnHeader2.Text = "Execute Time";
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(466, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 320);
      this.uiActionsControl.TabIndex = 1;
      // 
      // SqlLogControl
      // 
      this.Controls.Add(this.sqlListBox);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "SqlLogControl";
      this.Size = new System.Drawing.Size(616, 320);
      this.ResumeLayout(false);

    }
		#endregion

    private DateTime startTime;

    public void AddSql(string sql)
    {
      startTime = DateTime.Now;
      ListViewItem item = new ListViewItem(new string[] {sql, string.Empty});
      sqlListBox.Items.Add(item);
    }

    public void AddExecuteTime()
    {
      TimeSpan executeTime = DateTime.Now - startTime;
      sqlListBox.Items[sqlListBox.Items.Count-1].SubItems[1].Text = executeTime.ToString();
    }

    private string CopyToClipboard()
    {
      using (new CursorHandler())
      {
        StringBuilder builder = new StringBuilder();
        builder.Append(sqlListBox.Columns[0].Text);
        builder.Append("\t");
        builder.Append(sqlListBox.Columns[1].Text);
        builder.Append(Environment.NewLine);
      

        for (int i = 0; i < sqlListBox.Items.Count; i++)
        {
          builder.Append(sqlListBox.Items[i].SubItems[0].Text.ToString());
          builder.Append("\t");
          builder.Append(sqlListBox.Items[i].SubItems[1].Text.ToString());
          builder.Append(Environment.NewLine);
        }

        Clipboard.SetDataObject(builder.ToString(), true);
      }
      return "";
    }

    public event EventHandler<SqlExecutingEventArgs> ExecuteSelected;

    private string OnExecuteSelected()
    {
      if ((ExecuteSelected != null) && (sqlListBox.SelectedItems.Count > 0))
      {
        string sql = sqlListBox.SelectedItems[0].Text;
        ExecuteSelected(this, new SqlExecutingEventArgs(sql));
      }
      return "";
    }
	}
}
